//
//  ZYCFliterSingleChoiceView.m
//  ZDSC
//
//  Created by ZDTX on 2021/9/9.
//

#import "ZYCFliterSingleChoiceView.h"
#import "ZYCStuLeavePersonCell.h"
#import "ZYCRoleTreeModel.h"

@interface ZYCFliterSingleChoiceView ()<UITableViewDelegate,UITableViewDataSource>
{
    NSIndexPath *selectIndexPath;
}

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) ZYCRoleTreeModel *selectModel;

@end
@implementation ZYCFliterSingleChoiceView

- (void)refresh{
    [self.tableView reloadData];
}

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.tableView];
        [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
    }
    return self;
}



#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ZYCRoleTreeModel *model = self.dataSource[indexPath.row];
    ZYCStuLeavePersonCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ZYCStuLeavePersonCell class]) forIndexPath:indexPath];
    cell.model = model;
    if (selectIndexPath == indexPath) {
        cell.isSelect = YES;
    }else{
        cell.isSelect = NO;
    }
    return cell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    ZYCRoleTreeModel *model = self.dataSource[indexPath.row];
    ZYCStuLeavePersonCell *cell = [tableView cellForRowAtIndexPath:selectIndexPath];
    cell.isSelect = NO;
    selectIndexPath = indexPath;
    self.selectModel = model;

    if (self.selectStuBlock) {
        self.selectStuBlock(self.selectModel);
    };
    [self.tableView reloadData];
}


-(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01f;
}

-(CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 45.0f;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerClass:[ZYCStuLeavePersonCell class] forCellReuseIdentifier:NSStringFromClass([ZYCStuLeavePersonCell class])];
        if (@available(iOS 15.0, *)) {
            _tableView.sectionHeaderTopPadding = 0;
        }
    }
    return _tableView;
}


@end
